//
//  SeeImgManager.m
//  CloudCar
//
//  Created by TNG on 2018/2/1.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "SeeImgManager.h"
#import "SeeImgCollectionViewCell.h"

@implementation SeeImgManager

static NSString * reuseIdentifier = @"reuseIdentifier";

+(SeeImgManager *)manager
{
    static SeeImgManager * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[SeeImgManager alloc] initWithFrame:CGRectMake(0, 0 , WIDTH, HEIGHT)];
    });
    return manager;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor blackColor];
        [self addSubview:self.collectionView];
        [self addSubview:self.titleLabel];
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)]];
    }
    return self;
}

-(void)click
{
    [self hiddenView];
}

-(UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, kStatusBarH, WIDTH, 20)];
        _titleLabel.font =font(16);
        _titleLabel.textColor=[UIColor whiteColor];
        _titleLabel.textAlignment=NSTextAlignmentCenter;
    }
    return _titleLabel;
}

-(UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        UICollectionViewFlowLayout * floweLayout =[[UICollectionViewFlowLayout alloc]init];
        floweLayout.itemSize=CGSizeMake(WIDTH, HEIGHT);
        floweLayout.minimumLineSpacing=0;
        floweLayout.minimumInteritemSpacing=0;
        floweLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-kHomeLineH) collectionViewLayout:floweLayout];
        _collectionView.dataSource=self;
        _collectionView.delegate = self;
        _collectionView.bounces = NO;
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[SeeImgCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    }
    return _collectionView;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SeeImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [cell.img sd_setImageWithURL:[self.imgArr objectAtIndex:indexPath.row] placeholderImage:nil options:SDWebImageAllowInvalidSSLCertificates];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x/WIDTH;
    if (self.imgArr.count>1) {
        self.titleLabel.text = [NSString stringWithFormat:@"%d/%d",(int)page+1,(int)self.imgArr.count];
    }
}

-(void)hiddenView{
    [self removeFromSuperview];
}

-(void)showView
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

-(void)sendImgArr:(NSArray *)imgArr Page:(NSInteger)page
{
    self.imgArr = imgArr;
    WeakSelf;
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.collectionView reloadData];
    });
    if (page>=imgArr.count) {
        return;
    }
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:page inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

- (void)dealloc
{
    self.titleLabel = nil;
    self.collectionView = nil;
}

@end
